// subpkg/video-detail/video-detail.js
import { getVideoList } from '../../api/video'

Page({
  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    videoObj: {},
    videoList: [],
    isPlaying: false,
    currentIndex: 0
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getVideoListData(options.id)
  },

  async getVideoListData(id) {
    const res = await getVideoList(this.data.page)

    this.setData({
      videoList: res.data.feeds
    })

    const videoObj = res.data.feeds.find(item => item.id + '' === id)
    if (videoObj) {
      this.setData({
        videoObj
      }, () => {
        wx.setNavigationBarTitle({
          title: videoObj.title,
        })
      })
    }
  },
  // 播放
  play() {
    this.setData({
      isPlaying: true
    })
  },
  // 切换列表
  toggle(e) {
    const index = e.currentTarget.dataset.index
    const video = this.data.videoList[index]
    this.setData({
      currentIndex: index,
      videoObj: video
    }, () => {
      // 滚动到页面顶部
      wx.pageScrollTo({scrollTop: 0})

      wx.createVideoContext('myVideo').play()
    })
  }
})